﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.MultiEditingInPathEditor.SetLocationsProperties.CApSLPSetLocationsPropertiesEnabler
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System.Collections.Generic;
using System.Collections.ObjectModel;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.PrivateImplementationDetails;


namespace DnProcessSimulateCommands.MultiEditingInPathEditor.SetLocationsProperties;

public class CApSLPSetLocationsPropertiesEnabler : ITxCommandEnabler
{
  public bool Enable => this.ShouldEnable();

  private bool ShouldEnable()
  {
    bool flag = false;
    if (TxApplication.ActiveDocument.PlatformGlobalServicesProvider.IsDataLoaded())
      flag = this.HasLocations();
    return flag;
  }

  private bool HasLocations()
  {
    if (TxApplication.ActiveSelection.TotalCount == 0)
      return false;
    bool flag = false;
    TxTypeFilter txTypeFilter = new TxTypeFilter();
    txTypeFilter.AddIncludedType(typeof (ITxOrderedCompoundOperation));
    txTypeFilter.AddIncludedType(typeof (ITxRoboticLocationOperation));
    txTypeFilter.AddIncludedType(typeof (ITxCompoundOperation));
    foreach (ITxObject filteredItem in (Collection<ITxObject>) TxApplication.ActiveSelection.GetFilteredItems((ITxTypeFilter) txTypeFilter))
    {
      TxObjectList txObjectList = (TxObjectList) null;
      switch (filteredItem)
      {
        case ITxRoboticLocationOperation _:
          flag = true;
          break;
        case ITxOrderedCompoundOperation _:
          txObjectList = ((ITxObjectCollection) (filteredItem as ITxOrderedCompoundOperation)).GetAllDescendants((ITxTypeFilter) new TxTypeFilter(typeof (ITxRoboticLocationOperation)));
          break;
        default:
          txObjectList = ((ITxObjectCollection) (filteredItem as ITxCompoundOperation)).GetAllDescendants((ITxTypeFilter) new TxTypeFilter(typeof (ITxRoboticLocationOperation)));
          break;
      }
      if (!flag && txObjectList != null && ((Collection<ITxObject>) txObjectList).Count > 0)
        flag = true;
      if (flag)
        break;
    }
    return flag;
  }

  public static bool ShouldCheckLocationForCanBeModified() => TxTcUtilities.IsTcAnyPlatform;

  public static void ExtractLocationsFromSelection(
    out TxObjectList<ITxRoboticLocationOperation> editableLocations,
    out TxObjectList<ITxRoboticLocationOperation> readOnlyLocations)
  {
    editableLocations = new TxObjectList<ITxRoboticLocationOperation>();
    readOnlyLocations = new TxObjectList<ITxRoboticLocationOperation>();
    if (TxApplication.ActiveSelection.TotalCount == 0)
      return;
    bool flag = CApSLPSetLocationsPropertiesEnabler.ShouldCheckLocationForCanBeModified();
    TxTypeFilter txTypeFilter = new TxTypeFilter();
    txTypeFilter.AddIncludedType(typeof (ITxOrderedCompoundOperation));
    txTypeFilter.AddIncludedType(typeof (ITxRoboticLocationOperation));
    txTypeFilter.AddIncludedType(typeof (ITxCompoundOperation));
    TxObjectList filteredItems = TxApplication.ActiveSelection.GetFilteredItems((ITxTypeFilter) txTypeFilter);
    HashSet<ITxRoboticLocationOperation> locationOperationSet = new HashSet<ITxRoboticLocationOperation>();
    foreach (ITxObject itxObject in (Collection<ITxObject>) filteredItems)
    {
      if (itxObject is ITxRoboticLocationOperation)
      {
        ITxRoboticLocationOperation locationOperation = itxObject as ITxRoboticLocationOperation;
        if (!locationOperationSet.Contains(locationOperation))
        {
          locationOperationSet.Add(locationOperation);
          if (flag && !((ITxObject) locationOperation).CanBeModified)
            ((Collection<ITxRoboticLocationOperation>) readOnlyLocations).Add(locationOperation);
          else
            ((Collection<ITxRoboticLocationOperation>) editableLocations).Add(locationOperation);
        }
      }
      else if (itxObject is ITxObjectCollection && (itxObject is ITxOrderedCompoundOperation || itxObject is ITxCompoundOperation))
      {
        foreach (ITxRoboticLocationOperation allDescendant in (Collection<ITxObject>) (itxObject as ITxObjectCollection).GetAllDescendants((ITxTypeFilter) new TxTypeFilter(typeof (ITxRoboticLocationOperation))))
        {
          if (!locationOperationSet.Contains(allDescendant))
          {
            locationOperationSet.Add(allDescendant);
            if (flag && !((ITxObject) allDescendant).CanBeModified)
              ((Collection<ITxRoboticLocationOperation>) readOnlyLocations).Add(allDescendant);
            else
              ((Collection<ITxRoboticLocationOperation>) editableLocations).Add(allDescendant);
          }
        }
      }
    }
  }
}
